package com.hsck.order.kds.main.dialog.site

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.hsck.order.kds.base.utils.setOnSingleClickListener
import com.hsck.order.kds.databinding.SelectSiteDialogItemLayoutBinding
import com.hsck.order.kds.main.fragment.standard.bean.TestListBean

class SelectSiteDialogAdapter(val activity: Activity) :
    RecyclerView.Adapter<SelectSiteDialogAdapter.ItemVH>() {

    private var list: MutableList<TestListBean> = arrayListOf()
    private var lastSelectedPosition = -1
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemVH {
        return ItemVH(
            SelectSiteDialogItemLayoutBinding.inflate(
                LayoutInflater.from(parent.context), parent, false
            )
        )
    }

    inner class ItemVH(val binding: SelectSiteDialogItemLayoutBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(position: Int) {
            val item = list[position]
            binding.bean = item
            itemView.setOnSingleClickListener {
                if (lastSelectedPosition != -1 && lastSelectedPosition != position) {
                    list[lastSelectedPosition].itemSelected = false
                }
                item.itemSelected = !item.itemSelected
                lastSelectedPosition = position
                onSiteSelectedListener.onSiteSelected(item, position)
            }
        }
    }

    fun setListData(newList: MutableList<TestListBean>, append: Boolean = false) {
        if (append) {
            val startPosition = list.size
            list.addAll(newList)
            notifyItemRangeInserted(startPosition, newList.size)
        } else {
            list = newList.toMutableList()
            notifyDataSetChanged()
        }
    }

    fun addItemAfterId(newItem: TestListBean, targetId: String) {
        val targetIndex = list.indexOfFirst { it.id == targetId }
        if (targetIndex != -1) {
            list.add(targetIndex, newItem)
            notifyItemInserted(targetIndex)
        }
    }

    fun removeItemAfterId(targetId: String) {
        val targetIndex = list.indexOfFirst { it.id == targetId }
        if (targetIndex != -1 && targetIndex < list.size) {
            list.removeAt(targetIndex)
            notifyItemRemoved(targetIndex)
            if (targetIndex < list.size) {
                notifyItemRangeChanged(targetIndex, list.size - targetIndex)
            }
        }
    }

    fun removeItemAfterIndex(targetIndex: Int) {
        if (targetIndex < list.size) {
            list.removeAt(targetIndex)
            notifyItemRemoved(targetIndex)
            if (targetIndex < list.size) {
                notifyItemRangeChanged(targetIndex, list.size - targetIndex)
            }
        }
    }

    override fun onBindViewHolder(holder: ItemVH, position: Int) {
        holder.bindData(position)
    }

    override fun getItemCount(): Int {
        return list.size
    }


    private lateinit var onSiteSelectedListener: SiteSelectedListener
    fun setSiteSelectedListener(onSiteSelectedListener: SiteSelectedListener) {
        this.onSiteSelectedListener = onSiteSelectedListener
    }

    interface SiteSelectedListener {
        fun onSiteSelected(itemBean: TestListBean, position: Int)
    }
}